/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gr.anagnosg.image.imagemagick;

import gr.anagnosg.general.utils.FileUtils;
import static gr.anagnosg.image.imagemagick.ImageMagickUtils.initPaths;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
import org.im4java.process.ProcessExecutor;

/**
 *
 * @author anagnosg
 */
public class ParameterList {
    ArrayList params= new ArrayList();

    public ArrayList getParams() {
        return params;
    }

    public void setParams(ArrayList params) {
        this.params = params;
    }
    
    public void add(Param p){
        this.params.add(p);
    }
    public List<String> commandStringList(String commandp, String sourceFilename, String outputfile){
        List<String> output = new ArrayList<String>();
        output.add(commandp);
        for(int i =0 ; i <this.params.size();i++){
            Param p = (Param)this.params.get(i);
            if(!p.getType().equals(Constants.CHANGE_FORMAT_STR) && !p.getType().equals(Constants.MERGE_ACTION)){
                output.add( p.getAction());  
                output.add( p.getParamOption()); 
            }
        }
        
        output.add(sourceFilename);
        output.add(outputfile);
        return output;
    }
    public List<String> commandStringListMergable( ){
        List<String> output = new ArrayList<String>();
        
        for(int i =0 ; i <this.params.size();i++){
            Param p = (Param)this.params.get(i);
            if(!p.getType().equals(Constants.CHANGE_FORMAT_STR) && !p.getType().equals(Constants.MERGE_ACTION)){
                output.add( p.getAction());  
                output.add( p.getParamOption()); 
            }
        }
        
        
        return output;
    }
    
    
    public String commandString(String commandp, String sourceFilename, String outputfile){
        //find if it has rename action . 
        //for(){
        
        //}
        String output = "";
        if(this.params.size()>0){
            String command = commandp;
            Param paramChange = this.hasChangeFormat();
            if(paramChange!=null){
                //paramChange.get
                //File fT = new  File(outputfile);  
                //String outputParent = fT.getParent();
                String toChangeExt = paramChange.getValue();
                outputfile = FileUtils.changeExtension(outputfile, toChangeExt);
                //outputfile = outputParent + file
            }
            String paramStr = "";
            for(int i=0; i<this.params.size();i++){
                Param p = (Param)this.params.get(i);
                if(!p.getType().equals(Constants.CHANGE_FORMAT_STR) && !p.getType().equals(Constants.MERGE_ACTION) ){
                    paramStr += " "+ p.getValue();
                }
                
            }
            if(!(this.params.size()==1 && ((Param)this.params.get(0)).getType().equals(Constants.MERGE_ACTION))){
                output = command + " "+paramStr +" \"" +sourceFilename +"\" \"" + outputfile+"\"" ;
            }
        }
        return output;
    }
    
     public IMOperation commandOper(String imageSource, String imageDest){
       
        // create the operation, add images and operators/options
        IMOperation op = new IMOperation();
        //String imagePath = "/home/anagnosg/tmp/imagesTests/images/";
        op.addImage(imageSource);
        //op.resize(width,height);
        for(int i=0; i<this.params.size();i++){
            Param p = (Param)this.params.get(i);
            if(p.getFinishStatus()==ParamParser.STATUS_RESIZE_FINISH_PERCENT){
                //op.resi
            }
        }
        
        op.addImage(imageDest);
        // execute the operation
       return op;
    }
    
    
    
    
     public Param hasChangeFormat(){
        Param output = null;
        //change_format
        for(int i = 0 ; i < this.params.size();i++){
            Param pT  = (Param)this.params.get(i);
            if(pT.getType().equals(Constants.CHANGE_FORMAT_STR)){
                output = pT;
                break;
            }
        }
        return output;    
    }
     
     public Param hasAction(String action){
        Param output = null;
        //change_format
        for(int i = 0 ; i < this.params.size();i++){
            Param pT  = (Param)this.params.get(i);
            if(pT.getType().equals(action)){
                output = pT;
                break;
            }
        }
        return output;    
    }
}
